package com.yanqu.road.dao.impl.unioncampwar;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.unioncampwar.cross.CrossUnionCampWarGroupData;
import com.yanqu.road.utils.string.StringUtils;
import org.apache.mina.util.ConcurrentHashSet;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

public class UnionCampWarGroupDaoImpl extends TempDataObjectDao<CrossUnionCampWarGroupData> {


//    public static String getCrossUnionCampWarGroupDataSql = "select * from `t_c_union_camp_war_group` where weekId=? and `centerGroupId` in (?)";

    public Map<Integer, Map<Integer, CrossUnionCampWarGroupData>> getCrossUnionCampWarGroupDataMap(List<Integer> centerGroupIds, int weekId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, Map<Integer, CrossUnionCampWarGroupData>> dataMap = new ConcurrentHashMap<>();
        String sql = "select * from t_c_union_camp_war_group where weekId=? and centerGroupId in(" + StringUtils.listToString(centerGroupIds, ",") + ")";
        try {
            getDb().setConnName(DbNameStrategy.getCrossDb());
            call = getDb().getConnection().prepareCall(sql);
            int i = 1;
            call.setInt(i++, weekId);
            rs = call.executeQuery();
            while (rs.next()) {
                CrossUnionCampWarGroupData data = parse(rs);
                Map<Integer, CrossUnionCampWarGroupData> centerGroupMap = dataMap.get(data.getCenterGroupId());
                if (null == centerGroupMap) {
                    centerGroupMap = new ConcurrentHashMap<>();
                    dataMap.put(data.getCenterGroupId(), centerGroupMap);
                }
                centerGroupMap.put(data.getGroupId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }

    public static String updateCrossUnionCampWarGroupDataSql = "update `t_c_union_camp_war_group` set serverSet=? where centerGroupId=? and groupId=? and weekId=?";

    @Override
    protected boolean updateData(CrossUnionCampWarGroupData crossUnionCampWarGroupData) {
        crossUnionCampWarGroupData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossUnionCampWarGroupData.getServerSet())));
        para.put(i++, new DbParameter(Types.INTEGER, crossUnionCampWarGroupData.getCenterGroupId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossUnionCampWarGroupData.getGroupId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossUnionCampWarGroupData.getWeekId()));
        if (getDb().executeNoneQuery(updateCrossUnionCampWarGroupDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertCrossUnionCampWarGroupDataSql = "insert into `t_c_union_camp_war_group`(centerGroupId,groupId,weekId,serverSet) values (?,?,?,?)";

    @Override
    protected boolean addData(CrossUnionCampWarGroupData crossUnionCampWarGroupData) {
        crossUnionCampWarGroupData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, crossUnionCampWarGroupData.getCenterGroupId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossUnionCampWarGroupData.getGroupId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossUnionCampWarGroupData.getWeekId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossUnionCampWarGroupData.getServerSet())));
        if (getDb().executeNoneQuery(insertCrossUnionCampWarGroupDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected CrossUnionCampWarGroupData parseData(ResultSet rs) throws SQLException {
        CrossUnionCampWarGroupData crossUnionCampWarGroupData = new CrossUnionCampWarGroupData();
        crossUnionCampWarGroupData.setCenterGroupId(rs.getInt("centerGroupId"));
        crossUnionCampWarGroupData.setGroupId(rs.getInt("groupId"));
        crossUnionCampWarGroupData.setWeekId(rs.getInt("weekId"));
        crossUnionCampWarGroupData.setServerSet(JSON.parseObject(rs.getString("serverSet"),new TypeReference<Set<Long>>() {
        }));

        return crossUnionCampWarGroupData;
    }

}
